package com.kevin.action.builder;

import org.apache.commons.lang3.StringUtils;

public class SqlUtils{
	static String addOrderBy(String where, String orderColumnName) {
		if (where.toLowerCase().indexOf("order by") == -1) {
			where += " order by " + orderColumnName + " asc";
		} else if (where.toLowerCase().indexOf(" asc") == -1 && where.toLowerCase().indexOf(" desc") == -1) {
			// 有order by 但没 asc
			where += " asc";
		}
		return where;
	}
	
	static String reverseOrderBy(String where, String primaryKey) {
		int index = where.toLowerCase().indexOf("order by ");
		if (index > -1) {
			String orderby = StringUtils.EMPTY;
			String onlyOrderBy = where.substring(index).trim();// order by t_sort,t_course_code desc,t_id "
			where = where.substring(0, index);
			onlyOrderBy = onlyOrderBy.replace(",", " , ");
			String[] items = StringUtils.split(onlyOrderBy, ' ');
			orderby = StringUtils.EMPTY;
			boolean isAscOrDesc = false;
			for (String item : items) {
				switch (item.toLowerCase()) {
				case "asc":
					orderby += "desc ";
					isAscOrDesc = true;
					break;
				case "desc":
					orderby += "asc ";
					isAscOrDesc = true;
					break;
				case ";":// 遇上分号，如果前面没有升降序，默认为升，转为降。
					if (!isAscOrDesc) {
						orderby += "desc";
					}
					orderby += item;
					isAscOrDesc = false;
					break;
				case "":
					break;
				default:
					orderby += item + " ";
					isAscOrDesc = false;
					break;
				}
			}
			String lastItem = items[items.length - 1].toLowerCase();
			if (lastItem.equals("asc") && lastItem.equals("desc")) // 最后一项为空时，补desc
			{
				orderby += "desc";
			}
			return where + orderby.trim();

		} else {
			return where + " order by " + primaryKey + " desc";
		}

	}

	static String removeOneEqOne(String sql) {
		sql = sql.trim();
		if(sql == "where 1=1"){
			return StringUtils.EMPTY;
		}
		if(StringUtils.endsWith(sql, " and 1=1")){
			return StringUtils.substring(sql, sql.length()-8);
		}
		int i = StringUtils.indexOf(sql, "where 1=1");
		
		if(i>0){
			if(i == sql.length() -9){
				sql = StringUtils.substring(sql, sql.length() - 10);
			}else if(StringUtils.substring(sql, i+10,8).toLowerCase() == "order by"){
				//sql = StringUtils.remove(sql, i,10);
			}
		}
		return sql;
	}

	static String removeOrderBy(String where) {
		int index = where.toLowerCase().indexOf("order by");
		if (index > 0) {
			where = where.substring(0, index);
		}
		return where;
	}

	static String getOrderBy(String where, boolean reverse, String primaryKey) {
		String orderby = " order by " + primaryKey + " ";

		int index = where.toLowerCase().indexOf("order by ");
		if (index > -1) {
			if (where.toLowerCase().indexOf("asc") > -1 || where.toLowerCase().indexOf("desc") > -1) {
				where = where.substring(index);// 不需要反转，直接取排序内容返回。
				if (!reverse) {
					return where;
				}
				where = where.replace(",", " , ");
				String[] items = StringUtils.split(where, ' ');
				orderby = StringUtils.EMPTY;
				for (String item : items) {
					switch (item.toLowerCase()) {
					case "asc":
						orderby += "desc ";
						break;
					case "desc":
						orderby += "asc ";
						break;
					default:
						orderby += item + " ";
						break;
					}
				}
				return orderby.trim();
			} else if (reverse) {
				orderby = where + " desc";
			}
		} else {
			orderby += reverse ? "desc" : "asc";
		}
		return orderby.toLowerCase();
	}
}
